<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,maximum-scale=1.0" />
    <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/public.js" type="text/javascript" charset="utf-8"></script>
    <title>邮寄地址</title>
</head>
<body>
	<div id="address">
		<div class="addHeader">
			<span class="back"></span>
			<span class="title">邮寄地址</span>
			<span class="addBtn" onclick="add()">新建</span>
		</div>
		<div class="addressCon">

			<!-- <a href="javascript:void(0);" class="addList">
				<div class="list">
					<span class="listName"></span>
				</div>
				<div class="list">
					<span class="listPhone"><i class="phone"></i></span>
					<span class="listZipcode"><i class="zipCode"></i></span>
				</div>
				<div class="list">
					<span class="listAddress"><i class="address"></i></span>
				</div>
			</a> -->
		</div>
		<div class="addressNone">
			<img class="noneImg" src="images/image_shuju@2x.png"/>
			<span>您还未添加过邮寄地址哦~</span>
			<a href="newAddress.html" class="addAd"><img src="images/add@2x.png"/>新增地址</a>
		</div>
		<div class="del-box">
			<ul>
				<li onclick="mr()">设置默认</li>
				<li onclick="edit()">编辑</li>
				<li onclick="dele()">删除</li>
			</ul>
			<div class="close" onclick="closewin()">取消</div>
		</div>
		<div class="opacity-gray"></div>
	</div>
	<script>
		$(function () {
            //验证是否登陆
            var user_id = sessionStorage.getItem('userId');
            if (user_id == null) {
                alert('请先登录！');
                window.location.href = 'login.html';
            }
			//清除地址ID
			sessionStorage.removeItem('addId');
			sessionStorage.removeItem('addressId');
            sessionStorage.removeItem('province');
            sessionStorage.removeItem('district');
            sessionStorage.removeItem('city');
            //获取地址列表
			getAddress()
			
        });
		//获取地址列表
		function getAddress() {
            var user_id = sessionStorage.getItem('userId');
            $.ajax({
                url: url() + 'api/goods/address_list', //请求的url地址
                dataType: "JSON",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                type: "POST",   //请求方式
                data:{
                    user_id:user_id,
                },
                success: function (data) {
                    //请求成功时处理
                    if(data.status==1) {
                        // console.log(data);
                        var html1 = '';
                        $.each(data.result, function (i, item) {
							//开票选择地址（跳转）
                            if(sessionStorage.getItem('addressType') == null){
                                html1+='<div  class="addList">';
                            } else {
                                html1+='<div  class="addList" onclick="chucunaddid('+item.address_id+')">';
                            }
                            html1+='<div class="list">';
							if(item.is_default==1){
                                html1+='<span class="listName">'+item.consignee+'<span1 style="font-size: 12px;color: #006DD9;margin-left: 6px">默认</span1></span>';
							} else {
                                html1+='<span class="listName">'+item.consignee+'</span>';
							}
                            html1+='</div>';
                            html1+='<div class="list">';
                            html1+='<span class="listPhone">电话<i class="phone">'+item.mobile+'</i></span>';
                            html1+='<span class="listZipcode">邮编<i class="zipCode">'+item.zipcode+'</i></span>';
                            html1+='</div>';
                            html1+='<div class="list">';
                            html1+='<span class="listAddress">地址<i class="address">'+item.address+'</i></span>';
                            html1+='</div>';
                            html1+='<div class="more" onclick="more(this,'+item.address_id+')"></div>';
                            html1+='</div>';
                        });
                        $('.addressCon').html(html1);
                    } else {
                        console.log(data.msg);
						//判断里面有没有数据
						var invoList=$('.addressCon').children().length;
						if(invoList<=0){
							$('.addressCon').hide();
							$('.addressNone').show()
						}
					}
                }
            });
        }
	</script>
	<script type="text/javascript">

        function add(){
            window.location.href='newAddress.html'
        }
        $(".back").click(function(){
            history.go(-1)
        })
        function more(obj,id){
            $('.del-box').show();
            $('.opacity-gray').show();
            sessionStorage.setItem('addId',id);
        }
        // 取消
        function closewin(){
            $('.del-box').hide();
            $('.opacity-gray').hide();
        }
        // 删除
        function dele(){
            $('.del-box').hide();
            $('.opacity-gray').hide();
            var addId = sessionStorage.getItem('addId');
            $.ajax({
                url: url() + 'api/goods/addressDelete',    //请求的url地址
                dataType: "JSON",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                type: "POST",   //请求方式
                data:{
                    address_id:addId
                },
                success: function (data) {
                    //请求成功时处理
                    if(data.status==1){
                        window.location.href = "invoiceAddress.html";
                    } else {
                        console.log(data.msg);
                    }
                }
            });
        }
        // 编辑
        function edit(){
            $('.del-box').hide();
            $('.opacity-gray').hide();
            var addId = sessionStorage.getItem('addId');
            $.ajax({
                url: url() + 'api/goods/regionOne',    //请求的url地址
                dataType: "JSON",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                type: "POST",   //请求方式
                data:{
                    address_id:addId,
                },
                success: function (data) {
                    console.log(data);
                    //请求成功时处理
                    if(data.status==1){
                        sessionStorage.setItem('province',data.result.province);
                        sessionStorage.setItem('district',data.result.district);
                        sessionStorage.setItem('city',data.result.city);
                        window.location.href = "newAddress.html";
                    } else {
                        console.log(data.msg);
                    }
                }
            });
        }
        // 默认
        function mr(){
            var addId = sessionStorage.getItem('addId');
            $.ajax({
                url: url() + 'api/goods/addressSetDefault',    //请求的url地址
                dataType: "JSON",   //返回格式为json
                async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                type: "POST",   //请求方式
                data:{
                    address_id:addId
                },
                success: function (data) {
                    //请求成功时处理
                    if(data.status==1){
                        window.location.href = "invoiceAddress.html";
                    } else {
                        console.log(data.msg);
                    }
                }
            });
        }
        //获取地址id
		function chucunaddid(id) {
            sessionStorage.setItem('addressId',id);
            window.location.href = "invoice_info.html";
        }
	</script>
</body>
</html>
